import { createRouter, createWebHistory } from 'vue-router'
import WeatherComponent from '@/components/WeatherComponent.vue'
import WeatherInfoComponent from '@/components/WeatherInfoComponent.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/weather'
    },
    {
      path: '/weather',
      name: 'weather',
      component: WeatherComponent
    },
    {
      path: '/weather/:city',
      name: 'weatherInfo',
      props: route => ({ city: route.params.city, search: route.query.search }),
      component: WeatherInfoComponent,
    },
    {
      path: '/:pathMatch(.*)*',
      name: '404',
      component: () => import('../views/NotFoundView.vue'),
    },
  ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 在这里执行你的逻辑，比如输出一些日志
  console.log(`Navigating from ${from.fullPath} to ${to.fullPath}`)

  // 调用 next() 表示继续路由导航
  next()
})

export default router
